package com.yuw.action;

import com.yuw.bean.AdminInfoBean;
import com.yuw.service.IAdminInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 使用注解@RestController，表示该类是一个支持RESTFul编程的控制层的action类
 */
@RestController
public class AdminInfoRESTFulAction {

    /*
 使用注解获取Mybatis框架的Mapper接口的实现类的实例对象
 此处使用了spring框架的IOC和DI
 也使用了面向接口的编程软件原则
 */
    @Autowired  // 此处pring进行依赖注入时，使用的是按类型进行依赖注入
    private IAdminInfoService adminInfoService;

    /**
     * 使用ResponseEntity返回该类的实例对象，将结果集和http状态信息以构造参数的形式进行封装到ResponseEntity对象中，返回该对象即可
     *
     * @param adminInfoBean
     * @return
     */
    @RequestMapping(value = "/admininfo/", method = RequestMethod.GET)
    public ResponseEntity<List<AdminInfoBean>> doAjaxQueryAdminInfoByParams2(AdminInfoBean adminInfoBean) {

        // 打印查询参数实体类
        System.out.println("查询参数：" + adminInfoBean.toString());
        // 调用service方法进行查询业务逻辑处理
        List<AdminInfoBean> lstAdminInfos = adminInfoService.selectAdminInfoByParams(adminInfoBean);

        // 将返回的结果集数据和Http状态信息，存入ResponseEntity对象中
        ResponseEntity<List<AdminInfoBean>> responseEntity = new ResponseEntity<List<AdminInfoBean>>(lstAdminInfos, HttpStatus.OK);

        // 返回结果值
        return responseEntity;
    }

    /**
     * 使用ResponseEntity返回该类的实例对象，将结果集和http状态信息以构造参数的形式进行封装到ResponseEntity对象中，返回该对象即可
     *
     * @param adminId
     * @return 查询结果集
     */
    @RequestMapping(value = "/admininfo/{aid}", method = RequestMethod.GET, produces = MediaType
            .APPLICATION_JSON_VALUE)
    public ResponseEntity<AdminInfoBean> doAjaxQueryAdminInfoById(@PathVariable("aid") Integer adminId) {

        // 打印查询参数实体类
        System.out.println("查询参数：" + adminId);
        // 调用service方法进行查询业务逻辑处理
        AdminInfoBean adminInfoBean = adminInfoService.selectByPrimaryKey(adminId);

        // 将返回的结果集数据和Http状态信息，存入ResponseEntity对象中
        return new ResponseEntity<AdminInfoBean>(adminInfoBean, HttpStatus.OK);
    }

    @RequestMapping(value = "/admininfo/", method = RequestMethod.POST)
    public ResponseEntity<List<AdminInfoBean>> doAjaxSaveAdminInfo(AdminInfoBean adminInfoBean) {

        // 打印查询参数实体类
        System.out.println("查询参数：" + adminInfoBean.toString());
        // 新增用户信息，进行保存操作
        int i = adminInfoService.insertSelective(adminInfoBean);
        // 调用service，查询所的信息
        List<AdminInfoBean> lstAdminInfos = adminInfoService.selectAdminInfoByParams(new AdminInfoBean());

        // 将返回的结果集数据和Http状态信息，存入ResponseEntity对象中
        ResponseEntity<List<AdminInfoBean>> responseEntity = new ResponseEntity<List<AdminInfoBean>>(lstAdminInfos, HttpStatus.OK);

        // 返回结果值
        return responseEntity;
    }

    @RequestMapping(value = "/admininfo/{aid}", method = RequestMethod.PUT)
    public ResponseEntity<AdminInfoBean> doAjaxUpdateAdminInfo(@PathVariable("aid") Integer adminId, AdminInfoBean adminInfoBean) {

        // 打印查询参数实体类
        System.out.println("查询参数：" + adminInfoBean.toString());

        // 将修改记录的主键字段的值，设置给实体类的对应字段
        adminInfoBean.setAdminid(adminId);
        // 新增用户信息，进行保存操作
        int i = adminInfoService.updateByPrimaryKeySelective(adminInfoBean);
        // 调用service，查询所的信息
        AdminInfoBean adminInfoBUpdate = adminInfoService.selectByPrimaryKey(adminId);

        // 将返回的结果集数据和Http状态信息，存入ResponseEntity对象中
        ResponseEntity<AdminInfoBean> responseEntity = new ResponseEntity<AdminInfoBean>(adminInfoBUpdate, HttpStatus.OK);

        // 返回结果值
        return responseEntity;
    }

}
